<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\RlMember;
use support\Request;
use support\Response;
use plugin\admin\app\model\RlMemberWalletLog;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use const support\CORRECT;

/**
 * 用户资金日志明细 
 */
class RlMemberWalletLogController extends Crud
{
    
    /**
     * @var RlMemberWalletLog
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new RlMemberWalletLog;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('rl-member-wallet-log/index');
    }

    /**
     * 获取
     * @return Response
     */
    public function select(Request $request): Response
    {
        $page = (int)$request->get('page');
        $limit = (int)$request->get('limit', 10);
        $where['status']=1;
        $count=RlMemberWalletLog::count();
        $offset=($page-1)*$limit;
        $lists= RlMemberWalletLog::orderBy('id','desc')
            ->offset($offset)->limit($limit)->get()->toArray();
        //获取用户ID
        $memberIds=array_column($lists,'member_id');
        $members=RlMember::whereIn('id',$memberIds)->get()->toArray();
        $members=array_column($members,null,'id');
        //进行昵称头像追加
        foreach ($lists as &$list){
            $list['nickname']=$members[$list['member_id']]['nickname']??'';
            $list['avatar']=$members[$list['member_id']]['avatar']??'';
        }
        return json(['code' => 0, 'data' => $lists, 'count'=>$count,'msg' => '成功']);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('rl-member-wallet-log/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('rl-member-wallet-log/update');
    }

}
